<?php
/**
 * Created By PhpStorm
 * Author: patrick wu
 * Date: 2020/12/12
 * Time: 14:20
 */
namespace App\Service\Admin;

use App\Http\Model\Mysql\OrderRefundModel;
use App\Http\Model\Mysql\OrderModel;
use App\Http\Model\Mysql\OrderRefundProcessModel;
use Carbon\Carbon;

class RefundService extends BaseService
{
    /**
     * 售后订单列表
     * @param array $query
     * @param int $page
     * @param int|null $size
     * @return array
     */
    public function refundList(array $query = [], int $size = NULL, int $page = 1)
    {
        $model  =   OrderRefundModel::query();
        $where['is_delete'] =   OrderRefundModel::NOT_DELETE;

        /**
         * 搜索条件
         */

        //订单号
        if (isset($query['order_no']) && !empty($query['order_no'])) {
            $where['order_id']    =   OrderModel::where(['order_no' => $query['order_no']])->value('id') ?? 0;
        }

        //退款号码
        if (isset($query['refund_no']) && !empty($query['refund_no'])) {
            $where['refund_no']  =   trim($query['express_no']);
        }

        //退款类型
        if (isset($query['refund_type']) && !empty($query['refund_type'])) {
            $where['refund_type']  =   trim($query['refund_type']);
        }

        //售后状态
        if (isset($query['refund_status']) && !empty($query['refund_status'])) {
            switch ($query['refund_status']) {
                case 1:     //退款审核
                    $where['refund_type']   =   OrderRefundModel::ONLY_REFUND;
                    $where['is_agree']      =   OrderRefundModel::AGREE_WAIT;
                    break;

                case 2:     //退款同意
                    $where['refund_type']   =   OrderRefundModel::ONLY_REFUND;
                    $where['is_agree']      =   OrderRefundModel::AGREE_APPROVAL;
                    break;

                case 3:     //退款完成
                    $where['refund_type']   =   OrderRefundModel::ONLY_REFUND;
                    $where['is_agree']      =   OrderRefundModel::AGREE_APPROVAL;
                    $where['refund_status'] =   1;
                    break;

                case 4:     //退款拒绝
                    $where['refund_type']   =   OrderRefundModel::ONLY_REFUND;
                    $where['is_agree']      =   OrderRefundModel::AGREE_REJECT;
                    break;

                case 5:     //退货审核
                    $where['refund_type']   =   OrderRefundModel::ONLY_RETURN;
                    $where['is_agree']      =   OrderRefundModel::AGREE_WAIT;
                    break;

                case 6:     //退货同意
                    $where['refund_type']   =   OrderRefundModel::ONLY_RETURN;
                    $where['is_agree']      =   OrderRefundModel::AGREE_APPROVAL;
                    break;

                case 7:     //退货完成
                    $where['refund_type']   =   OrderRefundModel::ONLY_RETURN;
                    $where['is_agree']      =   OrderRefundModel::AGREE_APPROVAL;
                    $where['refund_status'] =   1;
                    break;

                case 8:     //退货拒绝
                    $where['refund_type']   =   OrderRefundModel::ONLY_RETURN;
                    $where['is_agree']      =   OrderRefundModel::AGREE_REJECT;
                    break;

                case 9:     //售后拒绝
                    $where['is_agree']      =   OrderRefundModel::AGREE_REJECT;
                    break;
            }
        }

        //申请售后时间
        if (isset($query['start_time']) && !empty($query['start_time'])) {
            $where[]    =   ['created_at', '>=', Carbon::parse($query['start_time'])->timestamp];
        }

        if (isset($query['end_time']) && !empty($query['end_time'])) {
            $where[]    =   ['created_at', '<=', Carbon::parse($query['end_time'])->timestamp];
        }

        $size   =   empty($size) ? self::PAGE_LIMIT : $size;
        $count  =   $model->where($where)->count();
        $list   =   $model->with([
                        'detail' => function ($query) {
                            $query->with([
                                'spec' => function ($query) {
                                    $query->select(['id', 'sku_code', 'spec_pic']);
                                }
                            ])->select(['id', 'product_id', 'item_id', 'product_price']);
                        }
                    ])->where($where)->select(['id', 'user_id', 'order_id', 'detail_id', 'refund_no', 'refund_price', 'refund_status', 'is_receive', 'is_agree', 'reason'])
                    ->offset(($page - 1) * $size)->limit($size)->get()->each(function (&$item){
                        //订单信息
                        $order_info   =   OrderModel::where(['id' => $item->order_id])->select(['id', 'order_no', 'created_at', 'buyer_name'])->first();
                        if (!empty($order_info)) {
                            $order_info->created_time   =   $order_info->created_at->toDateTimeString();
                            unset($order_info->created_at);
                        }
                        $item->order_info   =   $order_info;
                    });

        if ($list->isEmpty()) {
            return [true, []];
        } else {
            return [true, [
                'count' =>  $count,
                'list'  =>  $list->toArray(),
                'pages' =>  ceil($count / $size),
                'current'=> $page
            ]];
        }
    }

    /**
     * 订单详情
     * @param int $refund_id     订单ID
     * @return array
     */
    public function orderDetail(int $refund_id)
    {
        $info_result    =   OrderRefundModel::with([
                                'detail' => function ($query) {
                                    $query->with([
                                        'spec' => function ($query) {
                                            $query->select(['id', 'sku_code', 'spec_pic']);
                                        }
                                    ])->select(['id', 'product_id', 'item_id', 'product_price']);
                                }
                            ])->where(['id' => $refund_id, 'is_delete' => OrderRefundModel::NOT_DELETE])
                            ->select(['id', 'user_id', 'order_id', 'detail_id', 'refund_no', 'refund_price', 'refund_status', 'is_receive', 'is_agree', 'is_user_send', 'user_express_no', 'user_express', 'reason'])->first();

        if (empty($info_result)) {
            return [false, '订单不存在'];
        } else {
            $refund_process =   OrderRefundProcessModel::where(['refund_id' => $info_result->id])->select(['id', 'refund_id', 'operate_time', 'operate_type', 'operate_desc', 'operate_remark', 'operate_function'])
                                ->get()->each(function (&$item) {
                                    $item->operate_time =   Carbon::createFromTimestamp($item->operate_time)->toDateTimeString();
                                });
            $info_result->process   =   $refund_process->isEmpty() ? [] : $refund_process->toArray();
            //订单信息
            $order_info =   OrderModel::where(['id' => $info_result->order_id])->select(['id', 'order_no', 'created_at', 'buyer_name', 'pay_time', 'is_pay'])->first();
            if (!empty($order_info)) {
                $order_info->created_time   =   $order_info->created_at->toDateTimeString();
                unset($order_info->created_at);
                $order_info->pay_time       =   $order_info->pay_time == 0 ? 0 : Carbon::createFromTimestamp($order_info->pay_time)->toDateTimeString();
            }
            $info_result->order_info    =   $order_info;
            return [true, $info_result];
        }
    }

}
